<?php

/**
 * SiteController is the default controller to handle user requests.
 */
class SiteController extends SController {

    /**
     * @var string the default layout for the controller view. Defaults to 'column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $defaultAction='step1';
    public $uploadDirectoryName='uploads';
    public $continueUrl;
        
    /**
     * Index action is the default action in a controller.
     */
    public function actionStep1() {

        $root=str_replace('/setup', '', Yii::getPathOfAlias('webroot'));
        $dir=explode('/',$root);
        $dir='/'.array_pop($dir);
        $uploadPath=str_replace($dir,'',$root).DIRECTORY_SEPARATOR.$this->uploadDirectoryName;
        
        $paths = array(
            'assets' => $root . '/assets',
            'runtime' => $root . '/protected/runtime',
            'data' => $root . '/protected/data',
            'themes' => $root . '/themes',
            'images' => $uploadPath . '/images',
            'medias' => $uploadPath . '/medias',
            'data_dir' => $uploadPath . '/data',
            'data_json_dir' => $uploadPath . '/data/json',
            'data_xml_dir'=>$uploadPath.'/data/xml',
            //'avatars' => $uploadPath . '/avatars',
            'watermarks' => $uploadPath . '/watermarks',
            'templates' => $uploadPath . '/templates',
        );

        $this->continueUrl = Yii::app()->createUrl('../install');
        
        $this->render('index', array(
            'paths' => $paths,
        ));
    }
    
    public function isPathExist($path) {
        if (file_exists($path)) {
            return true;
        } elseif (mkdir($path, 0777, true)) {
            return true;
        }
        else
            return false;
    }

    public function isPathWritable($path) {
        if (is_writable($path)) {
            return true;
        } elseif (chmod($path, 0777)) {
            return true;
        }
        else
            return false;
    }    
}